#!/bin/bash

# chkconfig: 2345 90 60
# description: rq worker script (single worker process)

# example rq configuration file (to be placed in /etc/init.d)

# works on cent6

DAEMON=rq_worker
DAEMON_PATH=/opt/dcm/gen/
export UPLOADHOST=dcmsrv
VIRTUALENV=
LOGFILE=/var/log/${DAEMON}.log
PIDFILE=/var/run/${DAEMON}.pid

case "$1" in
start)
	printf "%-50s" "starting $DAEMON..."
	cd $DAEMON_PATH
	if [ ! -z "$VIRTUALENV" ]; then
		source $VIRTUALENV/bin/activate
	fi
	rq worker normal --pid $PIDFILE > ${LOGFILE} 2>&1 &
;;
status)
	if [ -f $PIDFILE ]; then
		PID=`cat $PIDFILE`
		if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
			printf "%s\n" "$DAEMON not running, but PID file ($PIDFILE) exists"
		else
			echo "$DAEMON running"
		fi
	else
		printf "%s\n" "$DAEMON not running"
	fi
;;
stop)
	printf "%-50s" "stopping $DAEMON"
	if [ -f $PIDFILE ]; then
		PID=`cat $PIDFILE`
		kill -HUP $PID
		rm -f $PIDFILE
	else
		printf "%s\n" "no PID file ($PIDFILE) - maybe not running"
	fi
;;
restart)
	$0 stop
	$0 start
;;

*)
	echo "Usage: $0 {status|start|stop|restart}"
	exit 1
esac

